//
//  ElarcPopViewController.swift
//  elarc01
//
//  Created by elarc on 16/4/29.
//  Copyright © 2016年 elarc. All rights reserved.
//

import Foundation

class ElarcPopViewController: UIViewController {
    
    var rootVC:UIViewController?
    var rootView:UIView?
    var popView:UIView?
    var maskView:UIView?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func createPopVCWithRootVC(rootVC:UIViewController,popView:UIView) -> Void {
        
        self.rootVC = rootVC
        self.popView = popView
        createUI()
    }
    
    func createUI() -> Void {
        
        self.view.backgroundColor = UIColor.redColor()
        rootVC?.view.frame = self.view.bounds
        rootVC?.view.backgroundColor = UIColor.whiteColor()
        rootView = rootVC?.view
        self.addChildViewController(rootVC!)
        self.view.addSubview(rootView!)
        
        maskView = {
            let maskView = UIView(frame: CGRectMake(0, 0, self.view.width, self.view.height))
            maskView.backgroundColor = UIColor.whiteColor()
            maskView.alpha = 0
            return maskView
        }()
    }
    
    func close() -> Void {
        
        var frame = popView?.frame
        frame?.origin.y += (popView?.frame.size.height)!
        
        UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
           
            self.maskView?.alpha = 0
            self.popView?.frame = frame!
            self.rootView?.layer.transform = self.firstTransform()
            }) { (finished) in
                UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
                self.rootView?.layer.transform = CATransform3DIdentity
                }) { (finished) in
                    self.popView!.removeFromSuperview()
                }
        }
    }
    
    func show() -> Void {
        UIApplication.sharedApplication().windows[0].addSubview(popView!)
        
        var frame = popView?.frame
        frame?.origin.y = self.view.bounds.size.height - (popView?.frame.size.height)!
        
        UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.rootView?.layer.transform = self.firstTransform()
        }) { (finished) in
            UIView.animateWithDuration(0.3, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
                self.rootView?.layer.transform = self.secondTransform()
                self.maskView?.alpha = 0.5
                self.popView?.frame = frame!
            }) { (finished) in
            }
        }
    }
    
    func firstTransform() -> CATransform3D {
        var t1 = CATransform3DIdentity
        t1.m34 = 1.0 / -900
        t1 = CATransform3DScale(t1, 0.95, 0.95, 1)
        t1 = CATransform3DRotate(t1, 15.0*3.14/180.0, 1, 0, 0)
        return t1
    }
    
    func secondTransform() -> CATransform3D {
        
        var t2 = CATransform3DIdentity
        t2.m34 = firstTransform().m34
        t2 = CATransform3DTranslate(t2, 0, self.view.frame.size.height * (-0.08), 0)
        t2 = CATransform3DScale(t2, 0.8, 0.8, 1)
        return t2
    }
    
    
    
    
    
}